package com.dudsbury.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTabbedPane;
import javax.swing.border.BevelBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.JSpinner;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextArea;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.JList;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import com.dudsbury.database.Booking;
import com.dudsbury.database.DatabaseExtension;
import com.dudsbury.database.MyCustomer;
import com.dudsbury.database.MyItem;
import com.dudsbury.database.MyPayment;
import com.dudsbury.database.MyRoom;
import com.dudsbury.other.Globals;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.SpinnerNumberModel;

public class BookingFrame extends JFrame {

	
	boolean newBooking = true;
	JComboBox addQty,remQty;
	private JPanel contentPane;
	private JTextField dateText;
	private JTextField startTxt;
	private JTextField endTxt;
	private JTextField depositText;
	private JTextArea detailsText;
	JComboBox<MyCustomer> custCombo;
	private Booking booking;
	private MyCustomer customer;
	private ArrayList<MyItem> items;
	private MyPayment payment;
	private MyRoom room;
	JComboBox comboBox, itemCombo,roomCombo;
	JCheckBox chckbxDay, chckbxNight;
	JSpinner spinner, spinner_1;
	JList<MyItem> selectedList, itemList;
	ArrayList<MyItem> bookingItems, selectItems;
	DefaultListModel<MyItem> itemModel, selectedModel;
	MyRoom[] roomList;
	ComboBoxModel<MyCustomer> cModel;
	ComboBoxModel<MyRoom> rModel;
	JFrame thisFrame = this;
	SearchScreen screen;
	
	public BookingFrame(Booking booking,MyRoom r,SearchScreen screen) {
		this(booking.getDate(),r);

		this.screen=screen;
		newBooking=false;
		//System.out.println("Booking: " + booking.getDate());
		this.booking = booking;

		try {
			customer = DatabaseExtension.getCustomersByBooking(booking
					.getBookingId().replaceAll("\n","").trim())[0];
			for (int i=0;i<custCombo.getModel().getSize();i++) {
				
				if (custCombo.getModel().getElementAt(i).getForename().equals(customer.getForename()))
			custCombo.setSelectedItem(custCombo.getModel().getElementAt(i));
			
			}
			System.out.println("CUSTOMER FOUND " + customer.getForename());
		} catch (Exception e) {
			System.out.println("NO CUSTOMER");
			// e.printStackTrace();
		}

	
		
		items = new ArrayList<MyItem>(Arrays.asList(DatabaseExtension
				.getItemsByBooking(booking.getBookingId())));

		try {
			for (int i = 0; i < comboBox.getModel().getSize(); i++) {
				if (booking.getBookingType().equals(
						(String) comboBox.getModel().getElementAt(i)))
					comboBox.setSelectedIndex(i);
			}
		} catch (Exception e) {
			System.out.println("Combobox error");
		}
		// dateText = new JTextField();
		// startTxt = new JTextField();
		dateText.setText(booking.getDate().toString(
				DateTimeFormat.forPattern("y-M-d")));

		startTxt.setText(booking.getStartTime());
		endTxt.setText(booking.getFinishTime());

		System.out.printf("Day: %b Night: %b\n", booking.isBookingDay(),booking.isBookingNight());
		chckbxDay.setSelected(booking.isBookingDay());
		chckbxNight.setSelected(booking.isBookingNight());

		spinner.setValue(booking.getOccupancyAdult());
		spinner_1.setValue(booking.getOccupancyChild());

		depositText.setText(String.valueOf(booking.getDeposit()));
		// Extra Details

		detailsText.setText(booking.getDetails());
		//System.out
				//.println("ARRAY: "
					//	+ DatabaseExtension.getItemsByBooking(booking
								//.getBookingId()).length);
		setSelectedItems();

		MyItem[] items = DatabaseExtension.getItemsByBooking(booking
				.getBookingId());

		for (MyItem i : items)
			selectedModel.addElement(i);
		selectedList.setModel(selectedModel);

	}

	/**
	 * @wbp.parser.constructor
	 */
	public BookingFrame(DateTime date,MyRoom r) {
		setTitle("Booking");
		setVisible(true);
		room = r;
		custCombo = new JComboBox<MyCustomer>();
		itemModel = new DefaultListModel<MyItem>();
		selectedModel = new DefaultListModel<MyItem>();
		MyCustomer cus[] = DatabaseExtension.getCustomers();

		custCombo.setModel(new DefaultComboBoxModel(cus));
	
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 709, 561);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(5, 5, 678, 513);

		JPanel panel = new JPanel();
		tabbedPane.addTab("Booking Details", null, panel, null);

		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null,
				null));

		JPanel panel_5 = new JPanel();
		panel_5.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null,
				null));

		JButton btnNewButton = new JButton("Create Customer");

		JLabel lblSelectCustomer = new JLabel("Select Customer:");
		
		JLabel lblCustomer = new JLabel("Customer");
		lblCustomer.setHorizontalAlignment(SwingConstants.CENTER);
		lblCustomer.setFont(new Font("Tahoma", Font.BOLD, 11));
		GroupLayout gl_panel_5 = new GroupLayout(panel_5);
		gl_panel_5.setHorizontalGroup(
			gl_panel_5.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_5.createSequentialGroup()
					.addGroup(gl_panel_5.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_5.createSequentialGroup()
							.addContainerGap()
							.addComponent(lblCustomer, GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE))
						.addGroup(gl_panel_5.createSequentialGroup()
							.addContainerGap()
							.addComponent(lblSelectCustomer)
							.addGap(18)
							.addComponent(custCombo, GroupLayout.PREFERRED_SIZE, 159, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap())
				.addGroup(Alignment.TRAILING, gl_panel_5.createSequentialGroup()
					.addContainerGap(96, Short.MAX_VALUE)
					.addComponent(btnNewButton)
					.addGap(86))
		);
		gl_panel_5.setVerticalGroup(
			gl_panel_5.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel_5.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblCustomer)
					.addPreferredGap(ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
					.addGroup(gl_panel_5.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblSelectCustomer)
						.addComponent(custCombo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnNewButton)
					.addContainerGap())
		);
		panel_5.setLayout(gl_panel_5);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new CustomerScreen().setVisible(true);
			}
		});

		JPanel panel_4 = new JPanel();
		panel_4.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null,
				null));
		
				JButton button_2 = new JButton("Save");
				button_2.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						
						saveBooking();
						
					}
				});
		
				JButton button_3 = new JButton("Clear");
				button_3.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						endTxt.setText("");
						startTxt.setText("");
						depositText.setText("");
					}
				});
		
				JButton button_4 = new JButton("Exit");
				button_4.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						
						thisFrame.dispose();
						
					}
				});
		
		JButton btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int option = JOptionPane.showConfirmDialog (null, "Are you sure?");
				
				if (option==JOptionPane.YES_OPTION) {
					if (booking!=null) {
					DatabaseExtension.deleteBooking(booking.getBookingId());
					}
					Globals.customTable.setRowData();
					thisFrame.dispose();
				}
			}
		});
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
						.addComponent(panel_3, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 647, Short.MAX_VALUE)
						.addGroup(gl_panel.createSequentialGroup()
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(panel_5, GroupLayout.PREFERRED_SIZE, 301, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.UNRELATED))
								.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
									.addComponent(button_2, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)))
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
									.addGap(10)
									.addComponent(button_3, GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE)
									.addGap(18)
									.addComponent(btnDelete, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
									.addComponent(button_4, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE))
								.addComponent(panel_4, GroupLayout.PREFERRED_SIZE, 336, Short.MAX_VALUE))))
					.addGap(21))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(panel_4, GroupLayout.PREFERRED_SIZE, 291, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(button_4)
								.addComponent(button_2)
								.addComponent(button_3)
								.addComponent(btnDelete)))
						.addComponent(panel_5, GroupLayout.PREFERRED_SIZE, 99, GroupLayout.PREFERRED_SIZE)))
		);

		JLabel lblDate = new JLabel("Date: ");

		dateText = new JTextField();
		dateText.setText("2013-4-12");
		dateText.setColumns(10);

		JLabel lblRoomBooked = new JLabel("Room Booked:");

		chckbxDay = new JCheckBox("Day");

		chckbxNight = new JCheckBox("Night");

		JLabel lblStartTime = new JLabel("Start Time: ");

		startTxt = new JTextField();
		startTxt.setColumns(10);

		endTxt = new JTextField();
		endTxt.setColumns(10);

		JLabel lblFinishTime = new JLabel("Finish Time:");

		JLabel lblAdults = new JLabel("Adults");

		spinner = new JSpinner();
		spinner.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));

		JLabel lblChildren = new JLabel("Children");

		spinner_1 = new JSpinner();
		spinner_1.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));

		JLabel lblBookingType = new JLabel("Booking Type: ");

		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] { "Wedding",
				"Wake", "Birthday Party", "Christmas Party", "Corporate Event",
				"Golf Event Meal", "Other" }));

		JLabel lblDeposit = new JLabel("Deposit:");

		depositText = new JTextField();
		depositText.setColumns(10);
		
		JLabel lblBooking = new JLabel("Booking");
		lblBooking.setHorizontalAlignment(SwingConstants.CENTER);
		lblBooking.setFont(new Font("Tahoma", Font.BOLD, 11));
		
		 roomCombo = new JComboBox();
		roomList = DatabaseExtension.getRooms();
		rModel = new DefaultComboBoxModel(roomList);
		roomCombo.setModel(rModel);
		
		for (int i=0;i<rModel.getSize();i++) {
			if (rModel.getElementAt(i).getRoomName().replaceAll("\n", "").trim().equals(room.getRoomName().replaceAll("\n", "").trim()))
			roomCombo.setSelectedIndex(i);
		}

		
		GroupLayout gl_panel_4 = new GroupLayout(panel_4);
		gl_panel_4.setHorizontalGroup(
			gl_panel_4.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_4.createSequentialGroup()
					.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_4.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
								.addComponent(lblBooking, GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
								.addGroup(gl_panel_4.createSequentialGroup()
									.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
										.addComponent(lblDeposit, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblBookingType)
										.addComponent(lblDate, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblFinishTime)
										.addComponent(lblRoomBooked)
										.addComponent(lblStartTime))
									.addGap(65)
									.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
										.addComponent(dateText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(depositText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(endTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(startTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addGroup(gl_panel_4.createSequentialGroup()
											.addComponent(spinner, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.UNRELATED)
											.addComponent(lblChildren)
											.addGap(20)
											.addComponent(spinner_1, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_panel_4.createSequentialGroup()
											.addPreferredGap(ComponentPlacement.RELATED)
											.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING, false)
												.addComponent(roomCombo, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
												.addGroup(gl_panel_4.createSequentialGroup()
													.addComponent(chckbxDay)
													.addGap(18)
													.addComponent(chckbxNight)))))
									.addGap(68))))
						.addGroup(gl_panel_4.createSequentialGroup()
							.addGap(23)
							.addComponent(lblAdults)))
					.addContainerGap())
		);
		gl_panel_4.setVerticalGroup(
			gl_panel_4.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_4.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblBooking)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_4.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblDeposit)
						.addComponent(depositText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel_4.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblBookingType)
						.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel_4.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblDate)
						.addComponent(dateText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(10)
					.addGroup(gl_panel_4.createParallelGroup(Alignment.BASELINE)
						.addComponent(roomCombo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblRoomBooked))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_4.createParallelGroup(Alignment.BASELINE)
						.addComponent(chckbxDay)
						.addComponent(chckbxNight))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_4.createParallelGroup(Alignment.BASELINE)
						.addComponent(startTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblStartTime))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel_4.createParallelGroup(Alignment.BASELINE)
						.addComponent(endTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblFinishTime))
					.addGap(20)
					.addGroup(gl_panel_4.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblAdults)
						.addComponent(spinner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(spinner_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblChildren))
					.addContainerGap(26, Short.MAX_VALUE))
		);
		panel_4.setLayout(gl_panel_4);

		JLabel lblBookingDetails = new JLabel("Booking Details");
		lblBookingDetails.setHorizontalAlignment(SwingConstants.CENTER);
		lblBookingDetails.setFont(new Font("Arial", Font.BOLD, 18));
		GroupLayout gl_panel_3 = new GroupLayout(panel_3);
		gl_panel_3.setHorizontalGroup(
			gl_panel_3.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_3.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblBookingDetails, GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panel_3.setVerticalGroup(
			gl_panel_3.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, gl_panel_3.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblBookingDetails, GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
					.addContainerGap())
		);
		panel_3.setLayout(gl_panel_3);
		panel.setLayout(gl_panel);

		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("Extra Details", null, panel_1, null);

		JPanel panel_6 = new JPanel();
		panel_6.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null,
				null));

		detailsText = new JTextArea();
		detailsText.setText("Extra Details about the booking");
		
				JButton button_5 = new JButton("Save");
				button_5.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						saveBooking();
					}
				});
		
				JButton button_6 = new JButton("Clear");
				button_6.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						detailsText.setText("");
					}
				});
		
				JButton button_7 = new JButton("Exit");
				button_7.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						thisFrame.dispose();
					}
				});
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGap(169)
							.addComponent(button_5, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(button_6, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(button_7, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addContainerGap()
							.addComponent(detailsText, GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addContainerGap()
							.addComponent(panel_6, GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE)))
					.addContainerGap())
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel_6, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(detailsText, GroupLayout.PREFERRED_SIZE, 277, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(button_7)
						.addComponent(button_6)
						.addComponent(button_5))
					.addContainerGap(53, Short.MAX_VALUE))
		);

		JLabel lblExtraDetails = new JLabel("Extra Details");
		lblExtraDetails.setHorizontalAlignment(SwingConstants.CENTER);
		lblExtraDetails.setFont(new Font("Arial", Font.BOLD, 18));
		GroupLayout gl_panel_6 = new GroupLayout(panel_6);
		gl_panel_6.setHorizontalGroup(
			gl_panel_6.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, gl_panel_6.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblExtraDetails, GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panel_6.setVerticalGroup(
			gl_panel_6.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_6.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblExtraDetails, GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
					.addContainerGap())
		);
		panel_6.setLayout(gl_panel_6);
		panel_1.setLayout(gl_panel_1);

		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("Select Items", null, panel_2, null);

		JPanel panel_7 = new JPanel();
		panel_7.setBounds(10, 11, 653, 58);
		panel_7.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null,
				null));

		JLabel label = new JLabel("Select Items");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setFont(new Font("Arial", Font.BOLD, 18));
		GroupLayout gl_panel_7 = new GroupLayout(panel_7);
		gl_panel_7.setHorizontalGroup(
			gl_panel_7.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, gl_panel_7.createSequentialGroup()
					.addContainerGap()
					.addComponent(label, GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panel_7.setVerticalGroup(
			gl_panel_7.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_7.createSequentialGroup()
					.addContainerGap()
					.addComponent(label, GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE)
					.addContainerGap())
		);
		panel_7.setLayout(gl_panel_7);

		JPanel panel_8 = new JPanel();
		panel_8.setBounds(10, 80, 277, 344);
		panel_8.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null,
				null));

		JLabel lblSelectItemType = new JLabel("Select Item Type:");
		lblSelectItemType.setHorizontalAlignment(SwingConstants.CENTER);
		lblSelectItemType.setFont(new Font("Tahoma", Font.BOLD, 11));

		itemList = new JList();
		itemList.setBorder(new LineBorder(new Color(0, 0, 0)));
		dateText.setText(date.toString(DateTimeFormat.forPattern("y-M-d")));
		itemCombo = new JComboBox();
		itemCombo.setModel(new DefaultComboBoxModel(new String[] {
				"Standard Meal Vegetarian", "Standard Meal", "Alcoholic Drink",
				"Non-Alcoholic Drink", "Other Items",
				"Buffet A Non-Vegetarian", "Buffet B Non-Vegetarian",
				"Buffet A Vegetarian", "Buffet B Vegetarian" }));
		itemCombo.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				setSelectedItems();

			}

		});
		
				JButton button = new JButton("Create New Item");
				button.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						new CreateItemScreen();
					}
				});
		
		JLabel lblQuantity = new JLabel("Quantity:");
		
		 addQty = new JComboBox<Integer>();
		 addQty.setModel(new DefaultComboBoxModel<Integer>(new Integer[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}));
		
				JButton button_1 = new JButton("Add selected");
				button_1.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						
						boolean exists = false;
						
						

						for (int i=0;i<selectedModel.size();i++) {
							if (selectedModel.getElementAt(i).getItemName().equals(itemList.getSelectedValue().getItemName())) {
								exists=true;
								selectedModel.getElementAt(i).quantity+= (Integer) addQty.getSelectedItem();
								thisFrame.repaint();
								thisFrame.revalidate();
								break;
							}
						}
						
						if (!exists) {
							MyItem hold = itemList.getSelectedValue().clone();
							hold.showQuantity=true;
							hold.quantity = (Integer) addQty.getSelectedItem();
							selectedModel.addElement(hold);
						}
					}
				});
		GroupLayout gl_panel_8 = new GroupLayout(panel_8);
		gl_panel_8.setHorizontalGroup(
			gl_panel_8.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_8.createSequentialGroup()
					.addGroup(gl_panel_8.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_8.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_panel_8.createParallelGroup(Alignment.LEADING)
								.addComponent(itemCombo, Alignment.TRAILING, 0, 250, Short.MAX_VALUE)
								.addComponent(itemList, GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)))
						.addGroup(gl_panel_8.createSequentialGroup()
							.addGap(79)
							.addComponent(lblQuantity, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(addQty, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel_8.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_panel_8.createParallelGroup(Alignment.LEADING)
								.addComponent(lblSelectItemType, GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
								.addGroup(gl_panel_8.createSequentialGroup()
									.addComponent(button)
									.addPreferredGap(ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
									.addComponent(button_1)))))
					.addContainerGap())
		);
		gl_panel_8.setVerticalGroup(
			gl_panel_8.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_8.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblSelectItemType)
					.addGap(8)
					.addComponent(itemCombo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(itemList, GroupLayout.PREFERRED_SIZE, 191, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel_8.createParallelGroup(Alignment.BASELINE)
						.addComponent(button)
						.addComponent(button_1))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel_8.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblQuantity)
						.addComponent(addQty, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(26))
		);
		panel_8.setLayout(gl_panel_8);

		JPanel panel_11 = new JPanel();
		panel_11.setBounds(403, 80, 260, 344);
		panel_11.setBorder(new BevelBorder(BevelBorder.RAISED, null, null,
				null, null));
		
				JButton button_10 = new JButton("Save");
				button_10.setBounds(160, 442, 96, 23);
				button_10.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						saveBooking();
					}
				});
		
				JButton button_12 = new JButton("Exit");
				button_12.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						thisFrame.dispose();
					}
				});
				button_12.setBounds(413, 442, 89, 23);

		JLabel lblItemsSelected = new JLabel("Items Selected:");
		lblItemsSelected.setHorizontalAlignment(SwingConstants.CENTER);
		lblItemsSelected.setFont(new Font("Tahoma", Font.BOLD, 11));

		selectedList = new JList();
		selectedList.setBorder(new LineBorder(new Color(0, 0, 0)));
		selectedList.setModel(selectedModel);

		JButton button_8 = new JButton("Remove Selected");
		button_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				int qty = Integer.valueOf((String) remQty.getSelectedItem());
				
				if (qty>=selectedList.getSelectedValue().quantity) {
				selectedModel.removeElement(selectedList.getSelectedValue());
				}else {
					selectedList.getSelectedValue().quantity-=qty;
					thisFrame.repaint();
					thisFrame.revalidate();
				}
			}
		});

		JButton button_9 = new JButton("Clear List");
		button_9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				selectedModel.clear();
			}
		});
		
		 remQty = new JComboBox();
		remQty.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}));
		
		JLabel lblQuantity_1 = new JLabel("Quantity:");
		GroupLayout gl_panel_11 = new GroupLayout(panel_11);
		gl_panel_11.setHorizontalGroup(
			gl_panel_11.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_11.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_11.createParallelGroup(Alignment.LEADING)
						.addGroup(Alignment.TRAILING, gl_panel_11.createSequentialGroup()
							.addComponent(button_8, GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(button_9, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
							.addContainerGap())
						.addGroup(Alignment.TRAILING, gl_panel_11.createSequentialGroup()
							.addComponent(lblQuantity_1)
							.addGap(32)
							.addComponent(remQty, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(72))
						.addGroup(Alignment.TRAILING, gl_panel_11.createSequentialGroup()
							.addComponent(lblItemsSelected, GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
							.addContainerGap())
						.addGroup(gl_panel_11.createSequentialGroup()
							.addComponent(selectedList, GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
							.addContainerGap())))
		);
		gl_panel_11.setVerticalGroup(
			gl_panel_11.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_11.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblItemsSelected)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(selectedList, GroupLayout.PREFERRED_SIZE, 219, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(gl_panel_11.createParallelGroup(Alignment.BASELINE)
						.addComponent(button_8)
						.addComponent(button_9))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel_11.createParallelGroup(Alignment.BASELINE)
						.addComponent(remQty, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblQuantity_1))
					.addContainerGap())
		);
		panel_11.setLayout(gl_panel_11);
		panel_2.setLayout(null);
		panel_2.add(panel_7);
		panel_2.add(panel_8);
		panel_2.add(button_10);
		panel_2.add(button_12);
		panel_2.add(panel_11);

		JPanel panel_13 = new JPanel();
		//tabbedPane.addTab("Payment", null, panel_13, null);
		GroupLayout gl_panel_13 = new GroupLayout(panel_13);
		gl_panel_13.setHorizontalGroup(gl_panel_13.createParallelGroup(
				Alignment.LEADING).addGap(0, 523, Short.MAX_VALUE));
		gl_panel_13.setVerticalGroup(gl_panel_13.createParallelGroup(
				Alignment.LEADING).addGap(0, 547, Short.MAX_VALUE));
		panel_13.setLayout(gl_panel_13);
		contentPane.setLayout(null);
		contentPane.add(tabbedPane);
		setVisible(true);
	}

	public void setSelectedItems() {

		int hold = itemCombo.getSelectedIndex();
		//System.out.println("Setting selected Items " + hold);
		selectItems = new ArrayList<MyItem>();
		MyItem items[] = DatabaseExtension.getItems();

		for (MyItem i : items) {
			if (i.getItemType() == hold) {
				selectItems.add(i);

			}
		}

		itemModel.clear();

		for (MyItem i : selectItems) {
			itemModel.addElement(i);
			//System.out.println("Adding Item");

		}
		itemList.setModel(itemModel);
	}
	
	public void saveBooking() {
		
		
		System.out.println("new Booking: " + newBooking);
		room  = (MyRoom) roomCombo.getSelectedItem();
		System.out.println("Saving To Room " + room + " on date : " + dateText.getText());
		String[] data = new String[15];
		
		try {
		data[0] = booking.getBookingId();
		}catch (Exception e) { data[0]=null;}
		data[1] = (String) comboBox.getSelectedItem();
		try {
		for (Booking b : DatabaseExtension.getBookings()) {
			if (booking!=null) {
			if (b.getBookingId().replaceAll("\n", "").trim().equals(booking.getBookingId().replaceAll("\n", "").trim()))
				continue;
			}
			if (DatabaseExtension.getRoomsByBooking(b.getBookingId()).length>0) {
System.out.printf("Checking Date: %s against: %s and Room:%s against: %s\n",b.getDateAsString(),dateText.getText(),DatabaseExtension.getRoomsByBooking(b.getBookingId())[0].getRoomName(),room.getRoomName());
			if (b.getDateAsString().equals(dateText.getText().trim()) && 
					DatabaseExtension.getRoomsByBooking(b.getBookingId())[0].getRoomName().equals(room.getRoomName()/*DatabaseExtension.getRoomsByBooking(booking.getBookingId())[0].getRoomName()*/)) {
				JOptionPane.showMessageDialog(null, "The Room is Booked on this date");
				return;
			}
		}
		}
		}catch (Exception e) {e.printStackTrace();};
		data[2] = dateText.getText();
		
		int day=0;
		int night =0;
		if (chckbxDay.isSelected()) {
			day=1;
		}
		if (chckbxNight.isSelected()) {
			night=1;
		}
		data[3] = String.valueOf(day);
		data[4] = String.valueOf(night);
		data[5] = startTxt.getText();
		data[6] = endTxt.getText();
		data[7] = String.valueOf(spinner.getValue());
		data[8] = String.valueOf(spinner_1.getValue());
		try {
			float f  = Float.valueOf(depositText.getText());
		}catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Deposit must be a numerical value");
			return;
		}
		data[9]  = depositText.getText();
		data[10] = "";
		data[11] = "";
		data[12] = "";
		data[13] = detailsText.getText();
		//data[14] = room.getRoomID();
		MyCustomer cus = (MyCustomer) custCombo.getSelectedItem();
		data[14] = cus.getCustomerID();
		Booking b = new Booking(data);
		/*
		if (!DatabaseExtension.checkBookingByRoomDate(b, room)) {
			JOptionPane.showMessageDialog(null, "Room is booked on this date");
			return;
		}
		*/
		int newId =  DatabaseExtension.saveBooking(b,newBooking);
		System.out.println("New ID: " + newId);
		ArrayList<MyItem> itemList = new ArrayList<MyItem>();
		
		for (int i=0;i<selectedModel.size();i++) {
			itemList.add(selectedModel.get(i));
		}			
		
	
		
		
		if (!newBooking) {
			DatabaseExtension.saveItemBooking(b.getBookingId(),itemList);
			DatabaseExtension.saveRoomBooking(room.getRoomID().replaceAll("\n", "").trim(), b,newBooking);
		}
		if (newBooking) {
			ArrayList<Booking> bookingList = DatabaseExtension.getBookings();
		
			b.setBookingId(String.valueOf(newId));
			
			DatabaseExtension.saveRoomBooking(room.getRoomID().replaceAll("\n", "").trim(), b,newBooking);
		DatabaseExtension.saveItemBooking(String.valueOf(bookingList.size()),itemList);
		//System.out.println("Room ID: " + room.getRoomID() + " Booking: " + booking.getBookingId());
		
		}
		
		
		Globals.customTable.setRowData();
		if (screen!=null)
		screen.search();
		this.dispose();
		
	}
}
